package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.ExamPackage;
import com.ruoyi.system.domain.ExamProjectExamintions;
import com.ruoyi.system.domain.HospitalArea;
import com.ruoyi.system.service.IExamPackageService;
import com.ruoyi.system.service.IHospitalAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.ExamProject;
import com.ruoyi.system.service.IExamProjectService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 体检套餐Controller
 * 
 * @author ruoyi
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/project")
public class ExamProjectController extends BaseController
{
    @Autowired
    private IExamProjectService examProjectService;
    @Autowired
    private IHospitalAreaService iHospitalAreaService;
    @Autowired
    private IExamPackageService examPackageService;
    /**
     * 查询体检套餐列表
     */
    @RequiresPermissions("system:project:list")
    @GetMapping("/list")
    public TableDataInfo list(ExamProject examProject)
    {
        startPage();
        List<ExamProject> list = examProjectService.selectExamProjectList(examProject);
        for (ExamProject project : list) {
            HospitalArea hospitalArea = iHospitalAreaService.selectHospitalAreaById(project.getEaseId());
            project.setEaseName(hospitalArea.getAddress());
            List<String> examPackage = examPackageService.getPackageName(project.getId());
            project.setExanName(examPackage);
            ExamPackage examPackages = examPackageService.selectExamPackageById(project.getPackageType());
            project.setPackageTypeName(examPackages.getPackageName());
        }
        return getDataTable(list);
    }

    /**
     * 导出体检套餐列表
     */
    @RequiresPermissions("system:project:export")
    @Log(title = "体检套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamProject examProject)
    {
        List<ExamProject> list = examProjectService.selectExamProjectList(examProject);
        ExcelUtil<ExamProject> util = new ExcelUtil<ExamProject>(ExamProject.class);
        util.exportExcel(response, list, "体检套餐数据");
    }

    /**
     * 获取体检套餐详细信息
     */
    @RequiresPermissions("system:project:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        ExamProject examProject = examProjectService.selectExamProjectById(id);
        List<Integer> examProjectExamintions = examProjectService.selectExamProjectExamintionsList(id);
        examProject.setExamUpdateByID(examProjectExamintions);
//        for (ExamProjectExamintions examProjectExamintion : examProjectExamintions) {
//            examProject.setExamUpdateByID(examProjectExamintion.getExamintionsId());
//        }
        return success(examProject);
    }

    /**
     * 新增体检套餐
     */
    @RequiresPermissions("system:project:add")
    @Log(title = "体检套餐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamProject examProject)
    {
        int i = examProjectService.insertExamProject(examProject);
        int projec = examProject.getId();
        List<Integer> examUpdateByID = examProject.getExamUpdateByID();
        for (Integer integer : examUpdateByID) {
            ExamProjectExamintions examProjectExamintions = new ExamProjectExamintions();
            examProjectExamintions.setExamintionsId(integer);
            examProjectExamintions.setProjecId(projec);
            examProjectService.insertExamProjectExamintions(examProjectExamintions);
        }
        return toAjax(i);
    }

    /**
     * 修改体检套餐
     */
    @RequiresPermissions("system:project:edit")
    @Log(title = "体检套餐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamProject examProject)
    {
        List<Integer> examUpdateByID = examProject.getExamUpdateByID();
        int deleteId = examProjectService.deleteExamProjectExamintionsList(examProject.getId());
        System.err.println(deleteId);
        int projec = examProject.getId();
        for (Integer examUpdateByID1 : examUpdateByID) {
            ExamProjectExamintions examProjectExamintions = new ExamProjectExamintions();
            examProjectExamintions.setExamintionsId(examUpdateByID1);
            examProjectExamintions.setProjecId(projec);
            examProjectService.insertExamProjectExamintions(examProjectExamintions);
        }
        return toAjax(examProjectService.updateExamProject(examProject));
    }

    /**
     * 删除体检套餐
     */
    @RequiresPermissions("system:project:remove")
    @Log(title = "体检套餐", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        examProjectService.deleteExamProjectExamintions(ids);
        return toAjax(examProjectService.deleteExamProjectByIds(ids));
    }
}
